// import enterpriseApi from "@/api/enterprise";
import { sdqygjssv1api } from '@/api/enterprise/index'
import store from "./index";
const state = {
    homeCollection: [],
    areaData: [],
    enterpriseParams: {

    },
    enterpriseList: [],
    enterpriseTotal: 0,
    savePickerList: {},
    enterpriseListLoading: false,
    gdPoiList: [],
    bdPoiList: [],
}

const mutations = {
    setGdPoiList(state, data) {
        state.gdPoiList = data
    },
    setBdPoiList(state, data) {
        state.bdPoiList = data
    },
    resetEnterpriseList(state, data) {
        state.enterpriseList = []
    },
    setHomeCollection(state, data) {
        state.homeCollection = data
    },
    setAreaData(state, data) {
        state.areaData = data
    },
    setEnterpriseList(state, data) {
        state.enterpriseList = state.enterpriseList.concat(data)
    },
    setEnterpriseParams(state, data) {
        state.enterpriseParams = data
    },
    setEnterpriseTotal(state, data) {
        state.enterpriseTotal = Number(data).toLocaleString();
    },
    changeEnterpriseCollect(state, data) {
        let index = state.enterpriseList.findIndex((v) => v.COMPANY_ID == data.company_id)
        if (index != -1) {
            state.enterpriseList[index].collectId = data.collectId
        }
    },
    setEnterprisePickerList(state, data) {
        state.savePickerList = data
    },
    setEnterpriseListLoading(state, data) {
        state.enterpriseListLoading = data
    }
}

const actions = {
    async getEnterPriseList({
        dispatch,
        commit,
        state,
    }) {
        let enterpriseParams = state.enterpriseParams
        if (!!!enterpriseParams["district"]) {
            commit("setEnterpriseParams", {
                ...enterpriseParams,
                district: "黄埔区"
            })
        }
        commit("setEnterpriseListLoading", true)
        // let res = await enterpriseApi.getEnterPriseList(state.enterpriseParams);
		sdqygjssv1api(state.enterpriseParams).then(res => {
			console.log('sdqygjssv1api', res)
			dispatch('getEnterPriseCount', state.enterpriseParams)
			console.log('state.enterpriseParams',state.enterpriseParams)
			if (state.enterpriseParams.pageNumber == 1) {
			    commit("resetEnterpriseList", true)
			}
			let {
			    content,
			    total
			} = res.data
			let list = []
			if (content) {
			    list = content.map((v) => {
			        return {
			            ...v,
			            ESTABLISH_DATE: v.ESTABLISH_DATE && v.ESTABLISH_DATE.split(" ")[0] || "",
			            // insuranceAmount: v.insuranceAmount ? v.insuranceAmount : 0 + "人",
			            capitalNum: v.CAPITAL ? Number(v.CAPITAL.replace(/[^\d.]/g, "")) : 0,
			            collectId: ""
			        };
			    });
			    let companyIds = list.map((v) => v.COMPANY_ID);
			    // let checkRes = await enterpriseApi.getCheckAddInUserCollect({
			    //     userId: store.state.user.userId,
			    //     companyIds
			    // });
			    // if (checkRes.errcode == 0) {
			    //     if (checkRes.data) {
			    //         checkRes = checkRes.data.content
			    //         list.forEach(v => {
			    //             let obj = checkRes.find((k) => k.companyId == v.COMPANY_ID)
			    //             if (!!obj) {
			    //                 v.collectId = obj.collectId
			    //             }
			    //         });
			    //     }
			    // }
			    commit("setEnterpriseList", list)
			
			}
			return list
		});
        
        
    },
    async getEnterPriseCount({
        commit,
        state
    }, playload) {
        let total = 0
        // let res = await enterpriseApi.getEnterPriseList({
        //     ...playload,
        //     searchType: "zs"
        // })
		let Paramsdata = {
			...playload,
			searchType: "zs"
		}
		sdqygjssv1api(Paramsdata).then(res => {
			if (res.errcode == 0 && res.data && res.data.content && res.data.content.length) {
			    let {
			        TOTAL
			    } = res.data.content[0]
			    total = TOTAL
			}
			commit("setEnterpriseTotal", total)
			commit("setEnterpriseListLoading", false)
		})
        
    }
}

const getters = {
    areaData: state => state.areaData || [],
    enterpriseParams: state => state.enterpriseParams || {},
    enterpriseList: state => state.enterpriseList || {},
    enterpriseTotal: state => state.enterpriseTotal,
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}
